﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BalanceCenter_Buy : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        tbUserName.Attributes.Add("onfocusout", "return RegexUserName()");

        if (!_Customer.isActivation)
        {
            PublicFunction.GoError(ErrorNumber.NotEnoughCompetence, "对不起，您没有足够的权限访问此页面", this.GetType().BaseType.FullName);

            return;
        }
        
        if (!this.IsPostBack)
        {
            labContent.Text = PublicFunction.GetOptionsBin("注册协议");

            FillddlBank();
            //FillddlUserType();

            long ParentID = Shove._Convert.StrToLong(this.Request["PID"], -1);
            int LR = Shove._Convert.StrToInt(this.Request["LR"], -1);

            //2008.04.21  添加1.满足消费ID自动生成：TT+4位随机数字
            //                2.密码默认:888888

            //System.Random r = new Random();
            //tbUserName.Text = "HC" + r.Next(10000000, 99999999).ToString();

            if (ParentID >= 0)
            {
                Customer tu = new Customer(_Site.ID);
                tu.ID = ParentID;

                string Return = "";
                int Result = tu.GetCustomerInformationByID(ref Return);
                if (Result == 0)
                {
                    tbParentUserID.Text = ParentID.ToString();
                    tbParentUserName.Text = tu.CustomerNO;
                    tbParentUserName.Enabled = false;
                }
            }

            tbCommendUserName.Text = _Customer.CustomerNO;      //默认登录上来的会员为推荐人

            if ((LR == 1) || (LR == 2))
            {
                if (LR == 1)
                {
                    rbL.Checked = true;
                    rbR.Checked = false;
                }

                if (LR == 2)
                {
                    rbL.Checked = false;
                    rbR.Checked = true;
                }
                rbL.Enabled = false;
                rbR.Enabled = false;
            }

            FillProvince();
            tbUserBirthday.Value = "1970-01-01";
            btnBuy.Attributes.Add("onclick", "return confirm('" + System.Web.HttpUtility.HtmlEncode("您确信输入的资料无误，并且立即注册吗？") + "');");

            this.tbUserPassword.Text = "888888";
            this.tbUserPassword_2.Text = "888888";
            this.tbUserPassword2.Text = "888888";
            this.tbUserPassword2_2.Text = "888888";

            tbUserPassword.Attributes.Add("value", tbUserPassword.Text);
            tbUserPassword_2.Attributes.Add("value", tbUserPassword_2.Text);
            tbUserPassword2.Attributes.Add("value", tbUserPassword2.Text);
            tbUserPassword2_2.Attributes.Add("value", tbUserPassword2_2.Text);
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        isAtFramePageLogin = true;                              //是框架页     
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url           
        RequestCompetences = Competences.BuildCompetencesList(Competences.SurrogateR);//页面权限

        base.OnLoad(e);
    }

    #endregion

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
        Panel3.Visible = false;
    }

    private void FillProvince()
    {
        DataTable dt = new DAL.Tables.tbl_Provinces().Open("", "", "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        Shove.ControlExt.FillDropDownList(ddlProvince, dt, "Name", "id");

        if (ddlProvince.Items.Count > 0)
        {
            ddlProvince.SelectedIndex = 0;
            FillCity(int.Parse(ddlProvince.SelectedValue));
        }
    }

    private void FillCity(int ProvinceID)
    {
        DataTable dt = new DAL.Tables.tbl_Citys().Open("", "ProvinceID = " + ProvinceID.ToString(), "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        Shove.ControlExt.FillDropDownList(ddlCity, dt, "Name", "id");


        if (ddlCity.Items.Count > 0)
        {
            ddlCity.SelectedIndex = 0;
        }
    }

    protected void ddlProvince_SelectedIndexChanged(object sender, System.EventArgs e)
    {
        FillCity(int.Parse(ddlProvince.SelectedValue));
    }

    private void FillddlBank()
    {
        DataTable dt = new DAL.Tables.tbl_Banks().Open("", "", "[Order]");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        Shove.ControlExt.FillDropDownList(ddlBankName, dt, "Name", "ID");
    }

    private void FillddlUserType()
    {
        DataTable dt = new DAL.Tables.tbl_CustomerPlan().Open("ID,Name", "[IsUse] = 1", "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        Shove.ControlExt.FillDropDownList(ddlUserType, dt, "Name", "ID");

    }

    protected void btnCheckUserName_Click(object sender, EventArgs e)
    {
        DataTable dt = new DAL.Tables.tbl_Customer().Open("", "[CustomerNo] = '" + tbUserName.Text.Trim() + "'", "");

        if (dt==null || dt.Rows.Count == 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "此用户名可以使用!");

            return;
        }
        else
        {
            Shove._Web.JavaScript.Alert(this.Page, "该用户名已经存在，请用其他用户名注册!");

            return;
        }
    }

    protected void btnBuy_Click(object sender, EventArgs e)
    {
        if (tbCommendUserName.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入推荐人消费ID。");
            return;
        }

        if (tbParentUserName.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入网络位置上级消费ID。");
            return;
        }

        if (tbUserName.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入消费ID。");
            return;
        }

        if (tbUserRealityName.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入消费ID真实姓名。");
            return;
        }

        if (tbUserPassword.Text == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入一级密码。");
            return;
        }

        if (tbUserPassword_2.Text != tbUserPassword.Text)
        {
            Shove._Web.JavaScript.Alert(this.Page, "输入的两次一级密码不一致。");
            return;
        }

        if (tbUserPassword2.Text == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入二级密码。");
            return;
        }

        if (tbUserPassword2_2.Text != tbUserPassword2.Text)
        {
            Shove._Web.JavaScript.Alert(this.Page, "输入的两次二级密码不一致。");
            return;
        }

        if (tbUserIDCardNumber.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入身份证号。");
            return;
        }

        /*
         * 
        if (tbUserEmail.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入电子邮件地址。");
            return;
        }

        if (tbUserTelephone.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入联系电话。");
            return;
        }
         * 
        */

        if (tbUserMobile.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入手机号码。");
            return;
        }

        if (tbUserBankNameDetail.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入详细开户行名称。");
            return;
        }

        if (tbUserBankCardNumber.Text.Trim() == "")
        {
            Shove._Web.JavaScript.Alert(this.Page, "请输入银行卡号。");
            return;
        }

        /*
        bool isActivation = false;           //是否虚单

        if(cbisActivation.Checked) 
        {
            isActivation = true;   
        }

        bool isNullPerformance = false;     //是否报空单

        if (cbisNullPerformance.Checked)
        {
            isNullPerformance = true;
        }

        if (isActivation == false && isNullPerformance == true)  //即是空单又是虚单
        {
            Shove._Web.JavaScript.Alert(this.Page, "空单不能是未激活状态。");
            return;
        }

        int Result = _Customer.GetCustomerInformationByID(ref Return);
        if (Result != 0)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        if (cbisActivation.Checked)
        {
            if (_Customer.Balance < Shove._Convert.StrToDouble(DAL.Functions.F_GetOption("每单金额"), 0))
            {
                Shove._Web.JavaScript.Alert(this.Page, "对不起，您的帐户余额不足。");

                return;
            }
        }

        int UserType = int.Parse(ddlUserType.SelectedValue);
        double Money = 0;
        DataTable dt = new DAL.Tables.tbl_Plan().Open("", "ID = " + UserType, "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "", this.GetType().BaseType.FullName);

            return;
        }

        try
        {
            Money = Shove._Convert.StrToDouble(dt.Rows[0]["Price"].ToString(), 0);
        }
        catch { }


        if (Money <= 0)
        {
            PublicFunction.GoError(ErrorNumber.Unknow, "购买金额错误", this.GetType().BaseType.FullName);

            return;
        }

        if (_Customer.Balance < Money)
        {
            Shove._Web.JavaScript.Alert(this.Page, "对不起，您帐户余额不足。");

            return;
        }
        */
        int ReturnValue = 0;
        string ReturnDescription = "";

        Customer Commend = new Customer(_Site.ID);
        Commend.CustomerNO = tbCommendUserName.Text.Trim();

        ReturnValue = Commend.GetCustomerInformationByNO(ref ReturnDescription);
        if (ReturnValue != 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "推荐人消费ID不存在。");

            return;
        }

        Customer Parent = new Customer(_Site.ID);
        Parent.CustomerNO = tbParentUserName.Text.Trim();
        ReturnDescription = "";
        ReturnValue = Parent.GetCustomerInformationByNO(ref ReturnDescription);
        if (ReturnValue != 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "网络位置上级消费ID不存在。");

            return;
        }

        short LR = 0;
        if (this.rbL.Checked == true)
        {
            LR = 1;
        }
        else if (this.rbR.Checked == true)
        {
            LR = 2;
        }

        if (!PublicFunction.CheckSystemRegisterState())
        {
            if (new DAL.Tables.tbl_Customer().GetCount("") >= 50)
            {
                PublicFunction.GoError(ErrorNumber.Unknow, "对不起您的系统未激活，不能继续报单", this.GetType().BaseType.FullName);

                return;
            }
        }

        System.Threading.Thread.Sleep(200);

        long UserID = 0;
        long SiteID = 0;

        int i = DAL.Procedures.P_Buy(_Customer.ID, Shove._Convert.ToDBC(tbUserName.Text.Trim()), Shove._Convert.ToDBC(tbUserRealityName.Text.Trim()), PublicFunction.EncryptPassword(tbUserPassword.Text), PublicFunction.EncryptPassword(tbUserPassword2.Text),
            rbUserSexM.Checked ? "男" : "女", tbUserBirthday.Value, int.Parse(ddlCity.SelectedValue), Shove._Convert.ToDBC(tbUserIDCardNumber.Text.Trim()), Shove._Convert.ToDBC(tbUserAddress.Text.Trim()), Shove._Convert.ToDBC(tbUserEmail.Text.Trim()),
            Shove._Convert.ToDBC(tbUserQQ.Text.Trim()), Shove._Convert.ToDBC(tbUserTelephone.Text.Trim()), Shove._Convert.ToDBC(tbUserMobile.Text.Trim()), Shove._Convert.StrToInt(ddlBankName.SelectedValue,0), tbUserBankNameDetail.Text.Trim(),
            Shove._Convert.ToDBC(tbUserBankCardNumber.Text.Trim()), Shove._Convert.StrToInt(ddlUserType.SelectedValue, 0), Parent.ID, LR, Commend.ID, cbAutoNext.Checked, cbisActivation.Checked, _Site.ID, ref UserID, ref SiteID, ref ReturnDescription);

        if (i < 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, ReturnDescription);

            return;
        }

        //检查该会员是否有网店，若有则复制一个站点，默认复制0号站点下的内容
        if (SiteID > 0)
        {
            PublicFunction.SiteAdd(SiteID);
        }

        Panel1.Visible = false;
        Panel2.Visible = false;
        Panel3.Visible = true;
    }

    /// <summary>
    /// 立即激活此会员
    /// </summary>
    protected void btnActiveUser_Click(object sender, EventArgs e)
    {
        //long UserID = ShoveEL._Convert.StrToLong(hUserID.Value, 0);

        //string Return = "";
        //int Result = 0;

        //if (DAL.Procedures.P_ActivationUser.Call(UserID, _User.ID, ref Result, ref Return) < 0)
        //{
        //    PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

        //    return;
        //}

        //if (Result < 0)
        //{
        //    ShoveEL._Web.JavaScript.Alert(this.Page, Return);

        //    return;
        //}

        //ShoveEL._Web.JavaScript.Alert(this.Page, "激活成功！", "Net.aspx?ID=" + UserID);
    }

    /// <summary>
    /// 回到组织结构图中
    /// </summary>
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("Net.aspx");
    }
}
